<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="info" icon="el-icon-refresh" @click="refresh"></el-button> 
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.wx_name" placeholder="昵称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" stripe>

                <el-table-column label="标题" prop="title" align="left"></el-table-column>
				<el-table-column label="面值" prop="mian_money" width="120" align="center">
                        <template #default="scope">
                               <span class="red"><text style="font-size: 9px;">¥</text>{{scope.row.mian_money}}</span>
                        </template>
                </el-table-column>
                <el-table-column label="使用要求" prop="wx_name" width="280" align="center">
					    <template #default="scope">
                               <span v-if="scope.row.require_type == 0">满{{scope.row.require_money}}元可用</span>
							   <span v-if="scope.row.require_type == 1">无限制</span>
                        </template>
				</el-table-column> 
                <el-table-column label="描述" prop="des" width="250" align="center">
                     
                </el-table-column>
                <el-table-column label="到期时间" prop="create_time" align="center" width="250">

					    <template #default="scope">
                               <span v-if="scope.row.time_type == 0">永久有效</span>
							   <span v-if="scope.row.time_type == 1">{{ scope.row.end_time }}到期</span>
							   <span v-if="scope.row.time_type == 2">领取后{{ scope.row.end_day }}天内有效</span>
                        </template>

				</el-table-column>
				<el-table-column label="状态" prop="status" width="80" align="center">
					<template #default="scope">
						<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="230">
					<template #default="scope">
						<el-button-group>
							
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-button text type="primary" size="small" @click="see(scope.row, scope.$index)">领取详细</el-button>
						</el-button-group>
					</template>
				</el-table-column>
				
                 
			</scTable>
		</el-main>
		<savecat-dialog v-if="dialog.save" ref="savecatDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></savecat-dialog>

		<el-dialog v-model="tan" title="领取详细" width="900px" :before-close="handleClose">
               <div style="padding: 20px;padding-top: 0;height: 70vh;overflow-y: auto">
						<el-table border :data="ling.rows">
							<el-table-column label="ID" prop="id" align="center"></el-table-column>
							<el-table-column label="头像" align="center" width="120">
								<template #default="scope">
									<div>
										<img class="feng" :src="scope.row.avatar" alt="">
									</div>
								</template>
							</el-table-column>
							<el-table-column label="昵称" prop="wx_name" align="center"></el-table-column>
							<el-table-column label="使用" prop="use_time" align="center">
								<template #default="scope">
									{{ scope.row.is_use == 1 ? '已使用' : '未使用' }}
								</template>
							</el-table-column>
							<el-table-column label="领取时间" prop="ling_time" align="center"></el-table-column>
						</el-table>
						<el-pagination background layout="prev, pager, next" :total="ling.total" :page-size="20" :current_page="page2" @current-change="ling_page" style="margin-top: 10px"/>
			   </div>
		</el-dialog>


	</el-container>
</template>

<script>
    import savecatDialog from './coupon_save'
	export default {
		name: 'mk_coupon',
		components: {
			savecatDialog
		},
		data() {
			return {
				tan:false,
				this_id:0,
				page2:1,
				ling:[],
				dialog: {
					save: false
				},
				apiObj: this.$API.system.mk.coupon,
				selection: [],
				search: {
					keyword: null
				}
			}
		},
		methods: {
			ling_page(e){
				 this.page2 = e
				 this.see({id:this.this_id})
			},
			async see(row){
				this.tan = true
				this.this_id = row.id
				var reqData = {id: row.id,'page':this.page2}
				var res = await this.$API.system.universal.list.get(reqData,'/mk/coupon_ling_list');
				if(res.code == 200){
					 this.ling = res.data
				}
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.savecatDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.savecatDialog.open('edit').setData(row)
				})
			},
			async audit(row,dex,t=1){
				var reqData = {id: row.id,'status':t}
				var res = await this.$API.demo.withdraw_post.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("操作成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id,'act':'del'}
				var res = await this.$API.system.universal.handle.post(reqData,'/mk/coupon_del');
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					var arr = this.selection
					var ob = []
					for(var i=0;i<arr.length;i++){
						  ob.push(arr[i].cid)
					}
					this.table_del({'cid':ob.join(",")})
				}).catch(() => {

				})
			},
			//表格选择后回调事件 
			selectionChange(selection){
				this.selection = selection;
			},
			//表格内开关
			async changeSwitch(val, row){
				//var res = await this.$API.demo.permission.post()
				var res = await this.$API.system.universal.handle.post({'id':row.id,'status':val},'/mk/coupon_save');
				if(res.code == 200){
					this.visible = false;
					this.$message.success("操作成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
            refresh(){
                this.$refs.table.reload()
            },
			//本地更新数据
			handleSaveSuccess(data, mode){
 
				if(mode=='add'){
					this.$refs.table.reload()
				}else if(mode=='edit'){
					this.$refs.table.reload()
				}
			}
		}
	}
</script>

<style>
.red{
    font-size: 15px;
    color:#ff3333
}
.feng{
    width: 40px;
    height: 40px;
    border-radius: 100%;
}
</style>

